package mutilcore;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.RecursiveTask;

import util.Tools;

public class StatisNameTask extends RecursiveTask<Integer> {

    private static final long serialVersionUID = 1L;
    private static final int THRESHOLD = 1000;
    public List<String> nameList;

    public StatisNameTask(List<String> nameList) {
        this.nameList = nameList;
    }

    @Override
    protected Integer compute() {
        int sum = 0;
        if (nameList.size() < THRESHOLD) {
            Map<String, Integer> userName = new HashMap<String, Integer>();
            userName.put("宝玉", 0);
            userName.put("黛玉", 0);
            userName.put("宝钗", 0);
            userName.put("凤姐", 0);
            userName.put("元春", 0);
            userName.put("迎春", 0);
            userName.put("探春", 0);
            userName.put("惜春", 0);
            userName.put("史湘云", 0);
            userName.put("秦可卿", 0);
            for (int i = 0; i < nameList.size(); i++) {
                Iterator<String> keyOfName = userName.keySet().iterator();
                while (keyOfName.hasNext()) {
                    String name = keyOfName.next();
                    Integer cnt = Tools.countMatches(nameList.get(i), name);
                    if (cnt > 0) {
                        userName.put(name, userName.get(name) + cnt);
                    }
                }
            }

            Iterator<String> keyOfName = userName.keySet().iterator();
            while (keyOfName.hasNext()) {
                String name = keyOfName.next();
                int vlaue = userName.get(name);
                Tools.userName.put(name, Tools.userName.get(name) + vlaue);
            }
        } else {
            int middle = nameList.size() / 2;
            StatisNameTask left = new StatisNameTask(
                    nameList.subList(0, middle));
            StatisNameTask right = new StatisNameTask(nameList.subList(middle,
                    nameList.size()));
            left.fork();
            right.fork();
            middle = left.join() + right.join();
        }
        return sum;
    }

}